package com.jr.controller;

import com.google.gson.Gson;
import com.jr.service.IEmpSalUsersService;
import com.jr.service.IEmployeesService;
import com.jr.service.impl.EmpSalUsersServiceImpl;
import com.jr.service.impl.EmployeesServiceImpl;
import com.jr.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/employees/*")
public class EmpSalUsersController extends HttpServlet {

    IEmployeesService ies = new EmployeesServiceImpl();
    IEmpSalUsersService iesus = new EmpSalUsersServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI(); // 获取完整请求路径  --- /aaa/projectInfo/showdt
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index); // 截取最后一个"/"之后的字符串作为方法名

        /* 反射获取并调用对应方法 */
        Class cla = this.getClass(); // 获取当前类的Class对象
        Method method = null;
        try {
            // 查找当前类中是否存在指定名称、且参数为HttpServletRequest和HttpServletResponse的方法
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);

            response.setContentType("application/json;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");

            // 调用找到的方法，this表示当前类实例，传递request和response作为参数
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace(); // 找不到对应方法的处理（这会导致404错误）
        } catch (IllegalAccessException e) {
            e.printStackTrace(); // 方法访问权限不足的处理
        } catch (InvocationTargetException e) {
            e.printStackTrace(); // 方法调用过程中抛出异常的处理
        }
    }

    // 展示数据
    public void showEmpList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PageHelper ph = new PageHelper();
        /*当前是第几页*/
        int indexpageEmp = Integer.parseInt(request.getParameter("indexpageEmp"));
        String searchEmp = request.getParameter("searchEmp");
        int i = ies.queryPageDataCount(); // 按名字查询的总条数
        ph.setPageIndex(indexpageEmp);// 当前页
        ph.setDataCount(i); // 分页查询 总条数
//        ph.setListIndex(ies.); // 分页查询 查询当前页面 展示集合

//        System.out.println("查询到的集合："+iues.queryIndexDataByName(search,ph));

        /*最终目的：*/
        response.getWriter().println(new Gson().toJson(ph));


    }


}
